package com.nbsaas.boot.controller.customer;

import com.nbsaas.boot.customer.api.apis.CustomerAddressApi;
import com.nbsaas.boot.customer.api.domain.request.CustomerAddressRequest;
import com.nbsaas.boot.customer.api.domain.request.CustomerAddressSearch;
import com.nbsaas.boot.customer.api.domain.response.CustomerAddressResponse;
import com.nbsaas.boot.customer.api.domain.simple.CustomerAddressSimple;
import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
*  对外控制器
*/
@RequiresAuthentication
@RestController
@RequestMapping("/customerAddress")
public class CustomerAddressController {


    @Resource
    private CustomerAddressApi customerAddressApi;

    @SearchData
    @RequestMapping("/search")
    public PageResponse <CustomerAddressSimple> search(@RequestBody CustomerAddressSearch request) {
        request.setCustomer(UserUtils.user().getId());
        return customerAddressApi.search(request);
    }

    @SearchData
    @RequestMapping("/list")
    public ListResponse<CustomerAddressSimple> list(@RequestBody CustomerAddressSearch request) {
        request.setCustomer(UserUtils.user().getId());
        return customerAddressApi.list(request);
    }

    /**
    * 添加数据
    *
    * @param request
    * @return
    */
    @CreateData
    @RequestMapping("/create")
    public ResponseObject <CustomerAddressResponse> create(@RequestBody @Validated(AddOperator.class) CustomerAddressRequest request) {
        request.setCustomer(UserUtils.user().getId());
        return customerAddressApi.create(request);
    }

   @UpdateData
   @RequestMapping("/update")
   public ResponseObject<CustomerAddressResponse> update(@RequestBody @Validated(UpdateOperator.class) CustomerAddressRequest request) {
       request.setCustomer(UserUtils.user().getId());
       return customerAddressApi.update(request);
   }

    @SearchData
    @RequestMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) CustomerAddressRequest request) {
        request.setCustomer(UserUtils.user().getId());
        return customerAddressApi.delete(request);
    }

    @SearchData
    @RequestMapping("/view")
    public ResponseObject <CustomerAddressResponse> view(@RequestBody @Validated(ViewOperator.class) CustomerAddressRequest  request) {
        return customerAddressApi.view(request);
    }
}